import { defineStore } from 'pinia'
import type { UserState } from '../type'
import { UserTokenEnum } from '../type'
import type { LoginFormState } from '@/layouts/user-layout/type'
// import { resetRouter } from '@/routes'
import { getToken, removeToken, setToken } from '@/utils/cache'
import { getUserInfo, login, userLogout } from '@/api/user'

const useUserStore = defineStore({
  id: 'user',
  state: (): UserState => ({
    token: getToken(UserTokenEnum.ACCESS_TOKEN) || '',
    refreshToken: getToken(UserTokenEnum.REFRESH_TOKEN) || '',
    userInfo: null,
  }),
  actions: {
    login(loginInfo: LoginFormState) {
      const { username, password } = loginInfo
      return new Promise((resolve, reject) => {
        login({
          username: username?.trim(),
          password,
        })
          .then((response) => {
            const { accessToken, refreshToken, userInfo } = response.data
            setToken(UserTokenEnum.ACCESS_TOKEN, accessToken)
            this.token = accessToken
            setToken(UserTokenEnum.REFRESH_TOKEN, refreshToken)
            this.refreshToken = refreshToken
            resolve(userInfo)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    getUserInfo() {
      return new Promise((resolve, reject) => {
        getUserInfo()
          .then((response) => {
            const data = response.data
            if (!data)
              return

            this.userInfo = data
            resolve(data)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    logout() {
      return new Promise((resolve, reject) => {
        userLogout()
          .then(() => {
            removeToken(UserTokenEnum.ACCESS_TOKEN)
            removeToken(UserTokenEnum.REFRESH_TOKEN)
            this.RESET_STATE()
            // resetRouter()
            resolve(null)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    resetToken() {
      return new Promise((resolve) => {
        removeToken(UserTokenEnum.ACCESS_TOKEN)
        removeToken(UserTokenEnum.REFRESH_TOKEN)
        this.RESET_STATE()
        resolve(null)
      })
    },

    async RESET_STATE() {
      this.$reset()
    },

    // changePassword(changePasswordForm: IChangePasswordReq) {
    //   const { currentPassword, newPassword, checkPassword } =
    //     changePasswordForm;
    //   return new Promise((resolve, reject) => {
    //     changePassword({
    //       currentPassword: currentPassword,
    //       newPassword: newPassword,
    //       checkPassword: checkPassword,
    //     })
    //       .then(() => {
    //         removeToken(UserTokenEnum.ACCESS_TOKEN);
    //         removeToken(UserTokenEnum.REFRESH_TOKEN);
    //         this.RESET_STATE();
    //         resetRouter();
    //         resolve(null);
    //       })
    //       .catch((error) => {
    //         reject(error);
    //       });
    //   });
    // },
  },
})

export default useUserStore
